

#include "SceneEditorFrame.h"
#include "SceneEditorDoc.h"
#include "SceneEditorView.h"

#include "QDictionary.h"
#include "QArray.h"

#include "ui_SceneEditor.h"


SceneEditorFrame::SceneEditorFrame(uint pluginID, const QString &filePath, QWidget *parent, Qt::WindowFlags flags)
    : EditorWindow(parent, flags)
    , mPluginID(pluginID)
    , mDoc(0)
    , ui(new Ui::SceneEditorFrame)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_DeleteOnClose, true);
    setWindowIcon(QIcon(":/Icons/scene.svg"));

    initMenu();
    initToolBar();
    initActions();

    initDocument(filePath);
    initDockWidget();
    initView();

//    QDictionary dict;

//    QDictionary bkgnd;
//    bkgnd.insert("x", 10);
//    bkgnd.insert("y", 20);
//    bkgnd.insert("name", "image.png");

//    QArray bkimages;
//    QVariant var;
//    var.setValue(bkgnd);
//    int type = var.userType();
//    bkimages.push_back(var);
//    var.setValue(bkimages);
//    dict.insert("BkgndImages", var);

//    QDictionary mask;
//    mask.insert("x", 30);
//    mask.insert("y", 40);
//    mask.insert("name", "image1.png");

//    QArray maskimages;
//    var.setValue(mask);
//    maskimages.push_back(var);
//    var.setValue(maskimages);
//    dict.insert("MaskImages", var);

//    dict.display();

//    QFile file("D:\\Users\\aaronwang\\Private\\Workspace\\private\\game\\XYOnline\\resource\\Demo\\Scene\\test.image");
//    file.open(QIODevice::WriteOnly);
//    dict.archive(&file);
//    file.close();
}

SceneEditorFrame::~SceneEditorFrame()
{
    mDoc->close();
    delete mDoc;

    delete mMenu;
    delete mToolBar;

    delete ui;
}

QString SceneEditorFrame::name() const
{
    if (mDoc != 0)
    {
        return mDoc->title();
    }

    return "";
}

uint SceneEditorFrame::getID() const
{
    return 0;
}

uint SceneEditorFrame::getPluginID() const
{
    return mPluginID;
}

QMenu *SceneEditorFrame::getMenu()
{
    return mMenu;
}

bool SceneEditorFrame::save()
{
    if (mDoc != 0)
    {
        mDoc->save();
    }

    return true;
}

void SceneEditorFrame::closeEvent(QCloseEvent *event)
{
    if (mDoc->isModified())
    {
        QMessageBox msgBox;
        msgBox.setText(tr("The document has been modified."));
        msgBox.setInformativeText(tr("Do you want to discard your changes?"));
        msgBox.setStandardButtons(QMessageBox::Discard | QMessageBox::Cancel);
        msgBox.setDefaultButton(QMessageBox::Cancel);
        int ret = msgBox.exec();
        switch (ret)
        {
        case QMessageBox::Save:
            // Save was clicked
            break;
        case QMessageBox::Discard:
            {
                emit closeEditor(this);
                QMainWindow::closeEvent(event);
            }
            // Don't Save was clicked
            break;
        case QMessageBox::Cancel:
            // Cancel was clicked
            break;
        default:
            // should never be reached
            break;
        }
    }
    else
    {
        emit closeEditor(this);

        QMainWindow::closeEvent(event);
    }
}

void SceneEditorFrame::initMenu()
{
    mMenu = new QMenu(tr("Scene"), this);
}

void SceneEditorFrame::initToolBar()
{
    mToolBar = new QToolBar(this);
    addToolBar(Qt::TopToolBarArea, mToolBar);
}

void SceneEditorFrame::initActions()
{
    QAction *action = 0;

    // 添加图片
    action = new QAction(QIcon(":/Icons/sign_plus.svg"), tr("Add Image"), this);
    mMenu->addAction(action);
    mToolBar->addAction(action);
    connect(action, SIGNAL(triggered()), SLOT(onActAddImage()));

    // 删除图片
    action = new QAction(QIcon(":/Icons/sign_minus.svg"), tr("Del Image"), this);
    mMenu->addAction(action);
    mToolBar->addAction(action);
    connect(action, SIGNAL(triggered()), SLOT(onActDelImage()));

    mMenu->addSeparator();
    mToolBar->addSeparator();
}

void SceneEditorFrame::initDocument(const QString &filePath)
{
    if (mDoc != 0)
    {
        delete mDoc;
        mDoc = 0;
    }

    mDoc = new SceneEditorDoc(this);
    connect(mDoc, SIGNAL(modified()), SLOT(onDocumentModified()));
    connect(mDoc, SIGNAL(saved()), SLOT(onDocumentSaved()));
    connect(mDoc, SIGNAL(opened()), SLOT(onDocumentOpened()));

    connect(mDoc, SIGNAL(imageInserted(ImageInfo*,QGraphicsPixmapItem*,int)), ui->mSceneEditorView, SLOT(onImageInserted(ImageInfo*,QGraphicsPixmapItem*,int)));
    connect(mDoc, SIGNAL(imageDeleted(int,qint64)), ui->mSceneEditorView, SLOT(onImageDeleted(int,qint64)));
    connect(mDoc, SIGNAL(imagePositionAdjusted(int,qint64,QPoint)), ui->mSceneEditorView, SLOT(onImagePositionAdjusted(int,qint64,QPoint)));
    connect(mDoc, SIGNAL(imageModeModified(int,qint64,int)), ui->mSceneEditorView, SLOT(onImageModeModified(int,qint64,int)));
    connect(mDoc, SIGNAL(imageOrderSwaped(int,int,qint64,qint64)), ui->mSceneEditorView, SLOT(onImageOrderSwaped(int,int,qint64,qint64)));

    connect(mDoc, SIGNAL(imageInserted(ImageInfo*,QGraphicsPixmapItem*,int)), ui->mTableProperties, SLOT(onImageInserted(ImageInfo*,QGraphicsPixmapItem*,int)));
    connect(mDoc, SIGNAL(imageDeleted(int,qint64)), ui->mTableProperties, SLOT(onImageDeleted(int,qint64)));
    connect(mDoc, SIGNAL(imagePositionAdjusted(int,qint64,QPoint)), ui->mTableProperties, SLOT(onImagePositionAdjusted(int,qint64,QPoint)));
    connect(mDoc, SIGNAL(imageModeModified(int,qint64,int)), ui->mTableProperties, SLOT(onImageModeModified(int,qint64,int)));
    connect(mDoc, SIGNAL(imageOrderSwaped(int,int,qint64,qint64)), ui->mTableProperties, SLOT(onImageOrderSwaped(int,int,qint64,qint64)));

    if (mDoc != 0)
    {
        mDoc->open(filePath);
    }
}

void SceneEditorFrame::initDockWidget()
{

}

void SceneEditorFrame::initView()
{
    QGraphicsScene *scene = new QGraphicsScene(this);
    QRectF rect = ui->mSceneEditorView->sceneRect();
    scene->setSceneRect(rect);
    ui->mSceneEditorView->init(scene);
}

void SceneEditorFrame::updateTitle(bool modifiedFlag)
{
    QString title = mDoc->title();
    if (modifiedFlag)
    {
        title += "*";
    }

    setWindowTitle(title);
}

void SceneEditorFrame::onDocumentModified()
{
    updateTitle(true);
}

void SceneEditorFrame::onDocumentSaved()
{
    updateTitle(false);
}

void SceneEditorFrame::onDocumentOpened()
{
    updateTitle(false);
}

void SceneEditorFrame::onActAddImage()
{
    QString path = mDoc->location() + QString("Image/");
    QStringList imageList = QFileDialog::getOpenFileNames(this, "Select Image Files", path, "Images (*.bmp *.png *.tga *.jpg)");
    QStringList::iterator itr = imageList.begin();
    while (itr != imageList.end())
    {
        QFileInfo fileInfo(*itr);
        QString filePath = fileInfo.absolutePath();
        QString fileTitle = fileInfo.baseName();
        QString fileExt = fileInfo.suffix();
        int pos = filePath.lastIndexOf("/");
        if (pos == -1)
        {
            pos = filePath.lastIndexOf("\\");
        }
        QString folder = filePath.mid(pos+1);
        QString name = folder + "/" + fileTitle + "." + fileExt;
        mDoc->insertImage(name, ImageBkgnd);
        ++itr;
    }
}

void SceneEditorFrame::onActDelImage()
{

}
